package com.huilian.iotbox.mqtt.server.base.handler.penetrate.vendmach;

import com.huilian.iotbox.aliyun.service.PubService;
import com.huilian.iotbox.data.cmd.PositiveDataFrameCMD;
import com.huilian.iotbox.data.cmd.VendmachPositiveDataFrameCMD;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.utils.IotBoxUtil;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.mqtt.server.base.handler.penetrate.PenetrateHandlerStrategy;
import com.huilian.iotbox.mqtt.server.base.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author 谢洛涛
 * @date 2024/1/12 17:51
 */
@Slf4j
@Service("penetrateHandlerServiceC913")
public class ParameterSettingsC913  implements PenetrateHandlerStrategy {

    @Value("${huilian.iot.productKey}")
    private String productKey;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PubService pubService;
    @Override
    public void process(String content, String deviceNo, Integer positionId) throws Exception {
        final String controlBoardCmd = content.substring(26, 28);
        DeviceVo deviceVo = deviceService.findOneDeviceByDeviceNo(deviceNo);
        if(controlBoardCmd.equals("02")){

        }else if(controlBoardCmd.equals("08")){
            DeviceDto deviceDto = new DeviceDto();
            deviceDto.setId(deviceVo.getId());
            BigDecimal lockTime =  BigDecimal.valueOf(IotBoxUtil.singleHex2Int(content.substring(28, 32)));
            lockTime  = (lockTime.multiply(BigDecimal.valueOf(10))).divide(BigDecimal.valueOf(1000));
            deviceDto.setLockTime(lockTime);
            deviceDto.setHierarchySequence(IotBoxUtil.singleHex2Int(content.substring(32, 34)));
            deviceDto.setFreightLaneSequence(IotBoxUtil.singleHex2Int(content.substring(34, 36)));
            deviceDto.setReverseSequence(IotBoxUtil.singleHex2Int(content.substring(36, 38)));
            deviceDto.setFeedbackSequence(IotBoxUtil.singleHex2Int(content.substring(38, 40)));
            Integer deliveryTime =  IotBoxUtil.singleHex2Int(content.substring(40, 44));
            deliveryTime = (deliveryTime*10)/1000;
            deviceDto.setDeliveryTime(deliveryTime);
            deviceService.updateDeviceById(deviceDto);
        }else if(controlBoardCmd.equals("04")){
            DeviceDto deviceDto = new DeviceDto();
            deviceDto.setId(deviceVo.getId());

            log.info("光眼开关{}", IotBoxUtil.singleHex2Int(content.substring(28, 30)));

            log.info("光检开关 灯控{}", IotBoxUtil.singleHex2Int(content.substring(30, 32)));
            deviceDto.setLightControl(IotBoxUtil.singleHex2Int(content.substring(30, 32))==1?true:false);

            log.info("灯控启动时间{}", IotBoxUtil.hex2Date(content.substring(32, 38)));
            deviceDto.setLampStartupTime(IotBoxUtil.hex2Date(content.substring(32, 38)));

            log.info("灯控停止时间{}", IotBoxUtil.hex2Date(content.substring(38, 44)));
            deviceDto.setLampStopTime(IotBoxUtil.hex2Date(content.substring(38, 44)));

            log.info("主板当前时间{}", IotBoxUtil.singleHex2Int(content.substring(44, 50)));

            log.info("制热制冷 温控{}", IotBoxUtil.singleHex2Int(content.substring(50, 52)));
            deviceDto.setTemperatureControl(IotBoxUtil.singleHex2Int(content.substring(50, 52)));

            log.info("设需启动温度值符号位{}", IotBoxUtil.singleHex2Int(content.substring(52, 54)));
            if(IotBoxUtil.singleHex2Int(content.substring(52, 54)) == 0){
                //负温度
                deviceDto.setCompressorStartingTemperature(IotBoxUtil.singleHex2Int(content.substring(54, 62)) / 10);
            }else{
                //正温度
                deviceDto.setCompressorStartingTemperature(IotBoxUtil.singleHex2Int(content.substring(54, 62)) / 10 * -1);
            }
            log.info("设罱启动温度值原始值{}", IotBoxUtil.singleHex2Int(content.substring(54, 62)));
            log.info("设罱启动温度值最终值{}", deviceDto.getCompressorStartingTemperature());

            log.info("设需停止温度值符号位{}", IotBoxUtil.singleHex2Int(content.substring(62, 64)));
            if(IotBoxUtil.singleHex2Int(content.substring(62, 64)) == 0){
                //负温度
                deviceDto.setCompressorStopTemperature(IotBoxUtil.singleHex2Int(content.substring(64, 72)) / 10);
            }else{
                //正温度
                deviceDto.setCompressorStopTemperature(IotBoxUtil.singleHex2Int(content.substring(64, 72)) / 10  * -1);
            }
            log.info("设罱停止温度值{}", deviceDto.getCompressorStopTemperature());


            log.info("当前温度值符号位{}", IotBoxUtil.singleHex2Int(content.substring(72, 74)));
            if(IotBoxUtil.singleHex2Int(content.substring(72, 74)) == 0){
                deviceDto.setCompressorCurrentTemperature(IotBoxUtil.singleHex2Int(content.substring(74, 82)) / 10 );
            }else{
                deviceDto.setCompressorCurrentTemperature(IotBoxUtil.singleHex2Int(content.substring(74, 82)) / 10 * -1);
            }

            log.info("当前温度值{}", deviceDto.getCompressorCurrentTemperature());

            log.info("风控{}", IotBoxUtil.singleHex2Int(content.substring(82, 84)));
            deviceDto.setFanControl(IotBoxUtil.singleHex2Int(content.substring(82, 84))==1?true:false);
            deviceService.updateDeviceById(deviceDto);
        }else if(controlBoardCmd.equals("09")){
            String command = PositiveDataFrameCMD.penetrate(VendmachPositiveDataFrameCMD.setCurrentTime(deviceVo.getDeviceNo()));
            pubService.pub(command, deviceVo.getDeviceNo(), productKey);
        }
    }
}
